
import LocalityQuery as LQ

reload(LQ)

class LQProximityDataBase:
    lq = None
    
    def __init__( self, _center, _dimensions, _divisions ):
        _halfsize = _dimensions * 0.5
        _origin = _center - _halfsize
        
        self.lq = LQ.lqCreateDatabase( _origin.x, _origin.y, _origin.z, \
                            _dimensions.x, _dimensions.y, _dimensions.z,  \
                            _divisions.x, _divisions.y, _divisions.z )
                            
    def allocateToken( self, _parentObject ):
        return tokenType( _parentObject, self )
        
    def getPopulation( self ):
        _count = 0
        _count = LQ.lqMapOverAllObjects( lq, counterCallBackFunction, _count )
        
        return _count
        
def counterCallBackFunction( _clientObject, _distanceSquared, _clientQueryState ):
    _counter = int( _clientQueryState )
    _counter += 1
    
class tokenType:
    proxy = None
    lq = None
    
    def __init__( self, _parentObject, _lqpd ):
        self.proxy = LQ.lqClientProxy()
        
        LQ.lqInitClientProxy( self.proxy, _parentObject )
        self.lq = _lqpd.lq
        
    def updateForNewPosition( self, _p ):
        LQ.lqUpdateForNewLocation( self.lq, self.proxy, _p.x, _p.y, _p.z )
        
    def findNeighbors( self, _center, _radius, _results ):
        LQ.lqMapOverAllObjectsInLocality( self.lq, _center.x, _center.y, _center.z, _radius, \
                                          perNeighborCallBackFunction, _results )
                                          
def perNeighborCallBackFunction( _clientObject, _distanceSquared, _clientQueryState ):
    _results = _clientQueryState
    _results.append( _clientObject )